[System.Reflection.Assembly]::LoadWithPartialName("System");
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration");

$env:12Hive = "${env:CommonProgramFiles}\Microsoft Shared\web server extensions\12"
$env:WinSDK = "$env:ProgramFiles\Microsoft SDKs\Windows\v6.0A\Bin"
$env:Net35 = "$env:SystemRoot\Microsoft.Net\Framework\v3.5"

#########################################################################################

## Gets local farm object
function global:get-farm
{
  return [Microsoft.SharePoint.Administration.SPFarm]::Local;
}

#########################################################################################

##Gets SPWebApplication based on given url
function global:get-webapplication([String]$url=$(throw 'Parameter -url is missing!'))
{
  return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url);
}

#########################################################################################

## Gets SPSite based on given url
function global:get-site([String]$url=$(throw 'Parameter -url is missing!'))
{
  return new-object Microsoft.SharePoint.SPSite($url);
}

#########################################################################################

## Gets SPWeb based on given url (will give web even if list or document url is given)
function global:get-web([String]$url=$(throw 'Parameter -url is missing!'))
{
  $site = get-site($url);
  return $site.OpenWeb();
}

#########################################################################################

## Disposes web object
function global:dispose-web($web=$(throw 'Parameter -web is missing!'), $parent=$false)
{
  $site = $web.Site;
  $web.Dispose();
  if($parent){$site.Dispose();}
}

#########################################################################################

## Gets a feature based on a string GUID (XXXXXX-XXXX...)
function global:get-feature([String]$gd=$(throw 'Parameter -gd is missing!'))
{   
  $guid = new-object System.Guid($gd); 
  return $global:farmfeatures[$guid];
}

#########################################################################################

## Gets a feature based on a GUID (XXXXXX-XXXX...)
function global:get-feature([System.Guid]$guid=$(throw 'Parameter -guid is missing!'))
{
  return $global:farmfeatures[$guid];
}

#########################################################################################

## Updates user information for given web
function global:update-userinfo([String]$url=$(throw 'Parameter -url is missing!'), [String]$username=$(throw 'Parameter -username is missing!'), [String]$newDisplayName=$null,[String]$newEmail=$null)
{
  $web = get-web($url);
  $user = $web.Users[$username];

  if($newDisplayName -ne $null)
  {
    $user.Name = $newDisplayName;
  }
  
  if($newEmail -ne $null)
  {
    $user.Email = $newEmail;
  }

  $user.Update();

  dispose-web($web);
}

#########################################################################################

## 2008 10 24 MV Gets all activated features in an entire web application...
function global:get-allactivatedfeaturesinwebapp([String]$url=$(throw 'Parameter -url is missing!'))
{
  $webApp = get-webapplication($url);

  foreach($site in $webApp.Sites)
  {
   Write-Output  "-------------------------"
   Write-Output  'SITE:'  $site.Url
   Write-Output  "-------------------------"
   Write-Output " "

   foreach($feature in $site.Features)
   {
      #Write-Host $feature.DefinitionId;
      Write-Output $farmfeatures[$feature.DefinitionId].DisplayName;
   }
   Write-Output " "

   foreach($web in $site.AllWebs)
   {
         Write-Output 'WEB:'  $web.Url;
         Write-Output "--------"
	 foreach($feature in $web.Features)
         {
            #Write-Host $feature.DefinitionId;
	    Write-Output $farmfeatures[$feature.DefinitionId].DisplayName;
         }
         Write-Output " "
   }

   Write-Output " "
   Write-Output " "
  }
}

#########################################################################################

## 2008 10 24 MV Gets all activated features in a website...
function global:get-allactivatedfeaturesinweb([String]$url=$(throw 'Parameter -url is missing!'))
{
	$web = get-web($url);
	foreach($feature in $web.Features)
	{
		Write-Output $farmfeatures[$feature.DefinitionId].DisplayName;
	}
}

#########################################################################################


##
function global:get-wsproxy([String]$url=$(throw 'Parameter -url is missing!'))
{
	#generate a tempfile path 
	$tempFileName = [System.IO.Path]::GetTempFileName();

	#generate the proxy code with wsdl.exe
	$null =& $env:WinSdk\wsdl.exe $url /n:Proxy /out:"$tempFileName"
	
	#comile the code
	$null =& $env:Net35\csc.exe /t:library /out:"$tempFileName.dll" "$tempFileName"
	
	#load the assembly
	$assembly = [System.Reflection.Assembly]::LoadFrom("$tempFileName.dll")
	
	#find the proxy within the assembly
	$proxyType = $assembly.GetTypes() | Where-Object { $_.IsSubclassOf([System.Web.Services.Protocols.SoapHttpClientProtocol]) -eq $true}
	
	#instantiate the proxy 
	$proxy = New-Object -TypeName $proxyType
	
	#return the proxy
	return $proxy;
}

#########################################################################################

## GLOBAL VARIABLES
$global:farm = get-farm;
$global:farmfeatures = $farm.FeatureDefinitions;

WRITE-OUTPUT ' '
WRITE-HOST -FOREGROUND RED 'use: get-help to display global variables and functions loaded'
WRITE-HOST -FOREGROUND YELLOW 'use: get-help "command-name" to display help on specific function'
WRITE-OUTPUT ' '

#########################################################################################

function global:get-help($cmd="blank")
{
  if($cmd -eq "blank")
  {
    WRITE-OUTPUT ' ' 
    WRITE-OUTPUT ***********************
    WRITE-HOST -FOREGROUND YELLOW 'GLOBAL VARIABLES LOADED'
    WRITE-OUTPUT ***********************
    WRITE-OUTPUT '[$env:12Hive], [$env:WinSDK], [$env:Net35], [$farm], [$farmfeatures]'
    WRITE-OUTPUT ' '
    WRITE-OUTPUT '**********************'
    WRITE-HOST -FOREGROUND CYAN '   FUNCTIONS LOADED   '
    WRITE-OUTPUT '**********************'
    WRITE-OUTPUT 'get-farm'
    WRITE-OUTPUT 'get-webapplication'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-site'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-web'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'dispose-web'
    WRITE-OUTPUT '   -web <SPWeb>, -parent <bool>'
    WRITE-OUTPUT 'get-feature -gd <String>'
    WRITE-OUTPUT 'get-feature -guid <System.Guid>'
    WRITE-OUTPUT 'update-userinfo'
    WRITE-OUTPUT '   -url <String>, -username <String>,'
    WRITE-OUTPUT '   -newDisplayName <String>, -newEmail <String>'
    WRITE-OUTPUT 'get-allactivatedfeaturesinwebapp'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-allactivatedfeaturesinweb'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-wsproxy'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-allSiteAdminsInWebApp'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-allSiteAdminsInWebAppXML'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-list'
    WRITE-OUTPUT '   -url <String>'
	 WRITE-OUTPUT '   -string <String>'
    WRITE-OUTPUT 'get-allEnterpriseWebsInWebAppXML'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-allEnterpriseWebsInWebAppFilteredXML'
    WRITE-OUTPUT '   -url <String>'
	 WRITE-OUTPUT '   -filterstring <String*>'
    WRITE-OUTPUT 'get-allEnterpriseSubWebsInWebXML'
    WRITE-OUTPUT '   -url <String>'
    WRITE-OUTPUT 'get-allThemesinWebAppXML'
    WRITE-OUTPUT '   -WebAppURL <String>'
    WRITE-OUTPUT 'get-allFilesInWebAppXML'
    WRITE-OUTPUT '   -WebAppURL <String>'
    WRITE-OUTPUT 'get-allbigImagesInWebAppXML'
    WRITE-OUTPUT '   -WebAppURL <String>'
    WRITE-OUTPUT 'get-allWebPropertiesInWebAppXML'
    WRITE-OUTPUT '   -WebAppURL <String>'
    WRITE-OUTPUT 'get-WebServiceCollection'
    WRITE-OUTPUT 'get-WebApplicationCollection'
    WRITE-OUTPUT 'get-WebApplicationCollectionNames'
	 WRITE-OUTPUT 'get-AllSiteCollectionSizes'
	 WRITE-OUTPUT 'get-AllSiteCollectionSizesXML'
	 WRITE-OUTPUT 'get-AllSiteAdminsXML'
	 WRITE-OUTPUT 'get-AllFilesSizesXML'
	 WRITE-OUTPUT 'disable-EntFeaturesInWeb'
	 WRITE-OUTPUT '   -WebURL <String>'
	 WRITE-OUTPUT 'get-AllWebPartsInWebXML'
	 WRITE-OUTPUT '   -WebURL <String>'
	 WRITE-OUTPUT 'get-AllWebPartsInFarmXML'

    WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "get-farm")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'GET-FARM'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Returns the current local farm object'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: '
     WRITE-OUTPUT '     <none>'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <Microsoft.SharePoint.Administration.SPFarm>'
     WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "get-webapplication")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'GET-WEBAPPLICATION'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Returns an SPWebApplication object for the given url'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: '
     WRITE-OUTPUT '     -url   <String>   Required   Url to web application'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <Microsoft.SharePoint.Administration.SPWebApplication>'
     WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "get-site")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'GET-SITE'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Returns an SPSite object for the given url'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: <none>'
     WRITE-OUTPUT '     -url   <String>   Required   Url to a site, web, or file'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <Microsoft.SharePoint.SPSite>'
     WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "get-web")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'GET-WEB'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Returns an SPWeb object for the given url'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: '
     WRITE-OUTPUT '     -url   <String>   Required   Url to a web or file'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <Microsoft.SharePoint.SPWeb>'
     WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "dispose-web")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'DISPOSE-WEB'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Disposes of the given SPWeb object to free up memory resources'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: '
     WRITE-OUTPUT '     -web     <Microsoft.SharePoint.SPWeb>   Required   SharePoint Web (subsite) object'
     WRITE-OUTPUT '     -parent  <Boolean>                      Optional   Is web object parent web Default=False'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <none>'
     WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "get-feature")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'GET-FEATURE'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Returns an SPFeatureDefinition object for a given string or System.Guid parameter'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: '
     WRITE-OUTPUT '   -gd     <String>        Required   Unique identifier for the registered feature   OR'
     WRITE-OUTPUT '   -guid   <System.Guid>   Required   Unique identifier for the registered feature'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <Microsoft.SharePoint.SPFeatureDefinition>'
     WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "update-userinfo")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'UPDATE-USERINFO'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Updates the SPUser object for a given site collection'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: '
     WRITE-OUTPUT '     -url              <String>   Required   Url to the site collection'
     WRITE-OUTPUT '     -username         <String>   Required   Full system username for the user:  DOMAIN\username'
     WRITE-OUTPUT '     -newDisplayName   <String>   Optional   The new display name of the user'
     WRITE-OUTPUT '     -newEmail         <String>   Optional   The new email address of the user'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <none>'
     WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "get-allactivatedfeatures")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'GET-ALLACTIVATEDFEATURES'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Returns all activated features for all sites/webs within given web application'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: '
     WRITE-OUTPUT '     -url   <String>   Required   Url to a web application'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <Microsoft.SharePoint.SPFeatureCollection>'
     WRITE-OUTPUT ' '
  }
  elseif($cmd -eq "get-wsproxy")
  {
     WRITE-OUTPUT ' '
     WRITE-HOST -FOREGROUND GREEN 'GET-WSPROXY'
     WRITE-OUTPUT '******************* '
     WRITE-OUTPUT 'Returns a proxy object based on a given url (.asmx) file'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Paramemters: '
     WRITE-OUTPUT '     -url   <String>   Required    Url to the asmx file to generate temporary proxy object'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Returns: <System.Web.Services.Protocols.SoapHttpClientProtocol>'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Do not forget to set the UseDefaultCredentials property to $true and reset the Url property'
     WRITE-OUTPUT ' '
     WRITE-OUTPUT 'Example:'
     WRITE-OUTPUT '$proxy = get-wsproxy "http://site/_vti_bin/Lists.asmx";'
     WRITE-OUTPUT '$proxy.UseDefaultCredentials = $true;'
     WRITE-OUTPUT '$proxy.Url = "http://site/_vti_bin/Lists.asmx";'
     WRITE-OUTPUT '$listcollection = $list.GetListCollection();'
  }
}

#########################################################################################

## 2008 09 25 MV - MV's first cmdlet...
function global:get-AllSiteAdminsInWebapp([String]$url=$(throw 'Parameter -url is missing!'))
{
  $webApp = get-webapplication($url);

  foreach($site in $webApp.Sites)
  {
   Write-Output  " "
   Write-Output  'SITE COLLECTION: ' $site.Url
	Write-Output " "
	Write-Output "SITES"
	Write-Output "===================="
	Write-Output " "
   foreach($web in $site.AllWebs)
   {
         Write-Output $web.Url;
			Write-Output "-----------------------------------"
			
			Write-Output "Site Admins:"
			foreach($user in $web.AllUsers | where-object {$_.IsSiteAdmin -eq $true} )
			{
				Write-Output  $user.LoginName;
			}
			Write-Output " "

   }
   Write-Output " "
  }
}

#########################################################################################

## 2008 09 25 MV - MV's first cmdlet...
function global:get-AllSiteAdminsInWebappXML([String]$url=$(throw 'Parameter -url is missing!'))
{
  $webApp = get-webapplication($url);

  foreach($site in $webApp.Sites)
  {
   Write-Output  " "
   Write-Output  "<sitecollection>" 
	Write-Output "<sitecollectionurl>" $site.Url
	Write-Output "</sitecollectionurl>"
   foreach($web in $site.AllWebs)
   {
         Write-Output "<site>"
			Write-Output "<siteurl>" $web.Url "</siteurl>"
			Write-Output "<siteadmins>"
			foreach($user in $web.SiteAdministrators )
			{
				Write-Output "<SiteAdmin>"
				Write-Output  $user.Name;
				Write-Output "</SiteAdmin>"
			}
			Write-Output "</SiteAdmins>"
			Write-Output "</site>"
   }
   Write-Output "</sitecollection>"
  }
}

#########################################################################################

## MV 20080927 - Gets a specific list in a specific web...
function global:get-list([String]$url=$(throw 'Parameter -url is missing!'), [String]$list=$(throw 'Parameter -list is missing!'))
{
	$web = get-web($url);
	$list = $web.Lists[$list];
	Write-Output "Item IDs:";
	foreach($item in $list.Items)
	{
		Write-Output $item.ID;
	}

  dispose-web($web);
}

#########################################################################################

## 20080928 MV - Gets all websites in all site collections in the web application with Enterprise Features activated...
function global:get-AllEnterpriseWebsInWebAppXML([String]$url=$(throw 'Parameter -url is missing!'))
{
  $webApp = get-webapplication($url);
  Write-Output "<webapp>"
  Write-Output "<webappURL>" $webapp.DisplayName "</webappURL>"
  Write-Output "<SiteCollections>"
  foreach($site in $webApp.Sites)
  {
   Write-Output  "<SiteCollectionURL>" $site.Url "</SiteCollectionURL>"
	Write-Output "<webs>"
   foreach($web in $site.AllWebs)
   {
			Write-Output "<web>"
			Write-Output "<webURL>" $web.URL "</webURL>"
			foreach($feature in $web.Features)
         {
				if ($farmfeatures[$feature.DefinitionId].DisplayName -eq "PremiumWeb")
				{
					Write-Output "<feature>Enterprise</feature>"
				}
         }
         Write-Output "</web>"
   }
	Write-Output "</webs>"
  }
  Write-Output "</SiteCollections>"
  Write-Output "</webapp>"
}

#########################################################################################

## 20080928 MV - Gets all websites in all site collections in the web application with Enterprise Features activated...
function global:get-AllEnterpriseWebsInWebAppFilteredXML([String]$WebAppURL=$(throw 'Parameter -WebAppURL is missing!'), [String]$FilterURL=$(throw 'Parameter -FilterURL is missing!'))
{
  $webApp = get-webapplication($WebAppURL);
  Write-Output "<webapp>"
  Write-Output "<webappURL>" $webapp.DisplayName "</webappURL>"
  Write-Output "<SiteCollections>"
  foreach($site in $webApp.Sites)
  {
   Write-Output  "<SiteCollectionURL>" $site.Url "</SiteCollectionURL>"
	Write-Output "<webs>"
   foreach($web in $site.AllWebs | where-object {$_.URL -like $FilterURL})
   {
			Write-Output "<web>"
			Write-Output "<webURL>" $web.URL "</webURL>"
			foreach($feature in $web.Features)
         {
				if ($farmfeatures[$feature.DefinitionId].DisplayName -eq "PremiumWeb")
				{
					Write-Output "<feature>Enterprise</feature>"
				}
         }
         Write-Output "</web>"
   }
	Write-Output "</webs>"
  }
  Write-Output "</SiteCollections>"
  Write-Output "</webapp>"
}

#########################################################################################

## 20081027 MV - Gets all subwebs in website with Enterprise Features activated...NOT WORKING YET - DO NOT USE !!!
## 20081027 MV - DOES NOT WORK - NEEDS TO BE RECURSIVE TO GET ALL SUBWEBS !...
function global:get-AllEnterpriseSubWebsInWebXML([String]$url=$(throw 'Parameter -url is missing!'))
{
	$web = get-web($url);
	Write-Output "<web>"
	Write-Output "<subwebs>"
	foreach($web in $web.Webs)
	{
			Write-Output "<web>"
			Write-Output "<webURL>" $web.URL "</webURL>"
			foreach($feature in $web.Features)
			{
				if ($farmfeatures[$feature.DefinitionId].DisplayName -eq "PremiumWeb")
				{
					Write-Output "<feature>Enterprise</feature>"
				}
			}
			Write-Output "</web>"
	}
	Write-Output "</subwebs>"
	Write-Output "</web>"
}

#########################################################################################

## 20081008 MV - Gets all themes of all websites in all site collections in the web application...
function global:get-allthemesinwebappXML([String]$WebAppURL=$(throw 'Parameter -WebAppURL is missing!'))
{
  $webApp = get-webapplication($WebAppURL);
  Write-Output "<webapp>"
  Write-Output "<webappURL>" $webapp.DisplayName "</webappURL>"
  Write-Output "<SiteCollections>"
  foreach($site in $webApp.Sites)
  {
   Write-Output  "<SiteCollectionURL>" $site.Url "</SiteCollectionURL>"
	Write-Output "<webs>"
   foreach($web in $site.AllWebs)
   {
			Write-Output "<web>" 
			Write-Output "<webURL>" $web.URL "</webURL>"
			Write-Output "<webTheme>" $web.Theme "</webTheme>"
         Write-Output "</web>"
   }
	Write-Output "</webs>"
  }
  Write-Output "</SiteCollections>"
  Write-Output "</webapp>"
}

#########################################################################################

## 20081016 MV - Gets all files of all websites in all site collections in the web application...
function global:get-allfilesXML([String]$WebAppURL=$(throw 'Parameter -WebAppURL is missing!'))
{
  $webApp = get-webapplication($WebAppURL);
  Write-Output "<webapp>"
  Write-Output "<webappURL>" $webapp.DisplayName "</webappURL>"
  Write-Output "<SiteCollections>"
  foreach($site in $webApp.Sites)
  {
   Write-Output "<SiteCollectionURL>" $site.Url "</SiteCollectionURL>"
	Write-Output "<webs>"
   foreach($web in $site.AllWebs)
   {
			Write-Output "<web>" 
			Write-Output "<webURL>" $web.URL "</webURL>"
			foreach($folder in $web.folders)
			{
				Write-Output "<Folder>"
				Write-Output "<FolderName>" $folder.name "</FolderName>"
				foreach($file in $folder.Files)
				{
					Write-Output "<File>"
					Write-Output "<FileName>" $file.name "</FileName>"
					Write-Output "<FileSize>" $file.properties["vti_filesize"] "</FileSize>" 
					Write-Output "</File>"
				}
				Write-Output "</Folder>"
			}
         Write-Output "</web>"
   }
	Write-Output "</webs>"
  }
  Write-Output "</SiteCollections>"
  Write-Output "</webapp>"
}

#########################################################################################

# 20081016 MV - Gets all images files over 5MB in all websites in all site collections in the web application...
function global:get-allbigimagesinwebappXML([String]$WebAppURL=$(throw 'Parameter -WebAppURL is missing!'))
{
  $webApp = get-webapplication($WebAppURL);
#  Write-Output "<webapp>"
#  Write-Output "<webappURL>" $webapp.DisplayName "</webappURL>"
#  Write-Output "<SiteCollections>"
  foreach($site in $webApp.Sites)
  {
#  Write-Output "<SiteCollectionURL>" $site.Url "</SiteCollectionURL>"
#	Write-Output "<webs>"
   foreach($web in $site.AllWebs)
   {
#			Write-Output "<web>" 
#			Write-Output "<webURL>" $web.URL "</webURL>"
			foreach($folder in $web.folders)
			{
#				Write-Output "<Folder>"
#				Write-Output "<FolderName>" $folder.name "</FolderName>"
				foreach($file in $folder.Files)
				{
					if ($file.properties["vti_filesize"] -gt 5000000)
					{
						Write-Output "<File>"
						Write-Output "<FileName>" $file.name "</FileName>"
						Write-Output "<FileURL>"  $file.URL "</FileURL>"
						Write-Output "<FileSize>" $file.properties["vti_filesize"] "</FileSize>" 
						Write-Output "</File>"
					}
				}
#				Write-Output "</Folder>"
			}
#         Write-Output "</web>"
   }
#	Write-Output "</webs>"
  }
#  Write-Output "</SiteCollections>"
#  Write-Output "</webapp>"
}

#########################################################################################

## 20081021 MV - Gets requested properties of all websites in all site collections in the web application...
function global:get-allWebPropertiesInWebAppXML([String]$WebAppURL=$(throw 'Parameter -WebAppURL is missing!'))
{
  $webApp = get-webapplication($WebAppURL);
  Write-Output "<webapp>"
  Write-Output "<webappURL>" $webapp.DisplayName "</webappURL>"
  Write-Output "<SiteCollections>"
  foreach($site in $webApp.Sites)
  {
   Write-Output  "<SiteCollectionURL>" $site.Url "</SiteCollectionURL>"
	Write-Output "<webs>"
   foreach($web in $site.AllWebs)
   {
		Write-Output "<web>" 			
		foreach($property in ($web | get-member) | where-object {$_.MemberType -eq "property"} )
		{
			# 2008 10 21 MV to All: DO NOT RUN YET - this code assumes all propertys are name-value pairs, but this is NOT valid !!!
			#			Write-Output "<PropertyName>" $property.name "</PropertyName>"
			#			Write-Output "<PropertyValue>" $property.value "</PropertyValue>"
		}			
		Write-Output "</web>"
   }
	Write-Output "</webs>"
  }
  Write-Output "</SiteCollections>"
  Write-Output "</webapp>"
}

#########################################################################################

#2008 10 21 MV -  Gets an SP WebServiceCollection object...
function global:get-webservicecollection()
{
  return new-object Microsoft.SharePoint.Administration.SPWebServiceCollection($farm);
}

#########################################################################################

#2008 10 21 MV -  Gets an SP WebApplicationCollection object... WORKS !!!
function global:get-webapplicationcollection()
{
  $ContentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
  return [Microsoft.SharePoint.Administration.SPWebApplicationCollection]$WebAppCollection = $ContentService.WebApplications; 
}

#########################################################################################

#2008 10 21 MV -  Prints out an SP WebApplicationCollection... WORKS !!
function global:get-webapplicationcollectionnames()
{
	$wac = get-webapplicationcollection
	foreach($webapp in $wac)
	{
		Write-Output $webapp.Name;
	}
}

#########################################################################################

## 2008 10 21 MV - Gets sizes of all site collections in the web application... - WORKS !!
function global:get-allsitecollectionsizes()
{
  #get a collection of all existing web applications...
  $webAppCol = get-webapplicationcollection;

  #loop through all the web apps...
  foreach($webApp in $webAppCol)
  {
	  #loop through all site collections in each web app...
	  foreach($site in $webApp.Sites)
	  {
	   #output the current size of the site collection...
		Write-Output  'SITE:'  $site.Usage.Storage
	  }  
  }
}

#########################################################################################

## 2008 10 21 MV - Gets sizes of all site collections in the web application... - WORKS !!
function global:get-allsitecollectionsizesXML()
{
  #get a collection of all existing web applications...
  $webAppCol = get-webapplicationcollection;
	Write-Output "<WebAppCollection>"
  #loop through all the web apps...
  foreach($webApp in $webAppCol)
  {
	  Write-Output "<WebApp>"
	  Write-Output "<WebAppName>" $webApp.Name "</WebAppName>"
	  #loop through all site collections in each web app...
	  foreach($site in $webApp.Sites)
	  {
	   #output the current size of the site collection...
		Write-Output "<SiteCollection>"
		Write-Output "<SiteCollectionRootURL>"   $site.URL "</SiteCollectionRootURL>"
		Write-Output  "<SiteCollectionSize>"  $site.Usage.Storage "</SiteCollectionSize>"
		Write-Output "</SiteCollection>"
	  }  
	  Write-Output "</WebApp>"
  }
  Write-Output "</WebAppCollection>"
}

#########################################################################################

## 2008 10 21 MV - gets all site admin in all webs in all sitecollection in all web apps in The Farm...
function global:get-AllSiteAdminsXML()
{
  #get a collection of all existing web applications...
  Write-Output "<WebAppCollection>"
  $webAppCol = get-webapplicationcollection;

  foreach($webApp in $webAppCol)
  {
	  Write-Output "<WebApp>"
	  Write-Output "<WebAppName>" $webApp.DisplayName "</WebAppName>"
	  foreach($site in $webApp.Sites)
	  {
		Write-Output  "<SiteCollection>" 
		Write-Output "<SiteCollectionURL>" $site.Url "</SiteCollectionURL>"
		foreach($web in $site.AllWebs)
		{
				Write-Output "<Site>"
				Write-Output "<SiteURL>" $web.Url "</SiteURL>"
				Write-Output "<SiteAdmins>"
				foreach($user in $web.SiteAdministrators )
				{
					Write-Output "<SiteAdmin>" $user.Name; "</SiteAdmin>"
				}
				Write-Output "</SiteAdmins>"
				Write-Output "</Site>"
		}
		Write-Output "</SiteCollection>"
	  }
	  Write-Output "</WebApp>"
  }
  Write-Output "</WebAppCollection>"
}

#########################################################################################

## 20081021 MV - SUMS all files of all websites in all web applications in the farm...
function global:get-AllFileSizesXML()
{
  Write-Output "<WebAppCollection>"
  $webAppCol = get-webapplicationcollection;
	foreach($webApp in $webAppCol)
	{
	  Write-Output "<webapp>"
	  Write-Output "<webappURL>" $webapp.DisplayName "</webappURL>"
	  Write-Output "<SiteCollections>"
	  foreach($site in $webApp.Sites)
	  {
		Write-Output "<SiteCollectionURL>" $site.Url "</SiteCollectionURL>"
		Write-Output "<webs>"
		foreach($web in $site.AllWebs)
		{
				Write-Output "<web>" 
				Write-Output "<webURL>" $web.URL "</webURL>"
				foreach($folder in $web.folders)
				{
					Write-Output "<Folder>"
					Write-Output "<FolderName>" $folder.name "</FolderName>"
					foreach($file in $folder.Files)
					{
						Write-Output "<FolderFile>"
						Write-Output "<FolderFileName>" $file.name "</FolderFileName>"
						Write-Output "<FolderFileSize>" $file.properties["vti_filesize"] "</FolderFileSize>" 
						Write-Output "</FolderFile>"
					}
					Write-Output "</Folder>"
				}
				foreach($file in $web.files)
				{
						Write-Output "<SiteFile>"
						Write-Output "<SiteFileName>" $file.name "</SiteFileName>"
						Write-Output "<SiteFileSize>" $file.properties["vti_filesize"] "</SiteFileSize>" 
						Write-Output "</SiteFile>"				
				}
				Write-Output "</web>"
		}
		Write-Output "</webs>"
	  }
	  Write-Output "</SiteCollections>"
	  Write-Output "</webapp>"
  }
  Write-Output "</WebAppCollection>"
}
#########################################################################################

## 20081024 MV - Gets all features of a website, displaying the DisplayName...
function global:disable-EntFeaturesInWeb([String]$WebURL=$(throw 'Parameter -WebURL is missing!'))
{
	# Get an object for the current website...
	$web = get-web($WebURL)

	# Get the collection of current (activated) features...

	# Find the feature which represents the Enterprise Features...
	foreach($feature in $web.Features)
	{
		if ($farmfeatures[$feature.DefinitionId].DisplayName -eq "PremiumWeb")
		{
			$entFeaturesGUID = $feature.DefinitionId
			Write-Host "entFeaturesGUID = " $entFeaturesGUID ", and entFeaturesGUID.DisplayName = " $farmfeatures[$entFeaturesGUID].DisplayName
			# Delete this feature from the SPWeb's feature collection...
			Write-Output "removeing this feature from the web.Features collection..."
			$web.Features.Remove($entFeaturesGUID);
			# Report success of deletion operation by redisplaying all current activated features (expecting ent features to be now missing...)
			Write-Output "Here is the current list of activated features for this SPWeb:"
			get-allactivatedfeaturesinweb($url)
		}
	}
}

#########################################################################################

## 20081028 MV - Returns all webparts on all files(webpart pages) of all websites in all site collections in all web applications in The Farm...
function global:get-AllWebPartsInWebXML([String]$WebURL=$(throw 'Parameter -WebURL is missing!'))
{
	$web = get-web($WebURL);
	Write-Output "<Web URL='" $web.URL "'>" 
	foreach($folder in $web.folders | where-object{$_.Name -eq "Pages"})
	{
		Write-Output "<Folder Name='" $folder.Name "'>"
		foreach($file in $folder.Files)
		{
			Write-Output "<FolderFile Name= '" $file.Name "'>"

			#get the file's URL...
			$FileURL = $file.URL

			#get a collection of all the webparts on the page using SPWeb.GetLimitedWebPartManager() method...
			$scope = [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared;
			$WebPartManager = $web.GetLimitedWebPartManager($FileURL, $scope);
			$WebPartCollection = $WebPartManager.WebParts;

			#loop through the web part collection printing out the "WebBrowsableObject" (full name) of each web part...
			foreach($WebPart in $WebPartCollection)
			{
				Write-Output "<FolderWebPart>"
				Write-Output "<FolderWebPartPedigree>" $WebPart.WebBrowsableObject.ToString() "</FolderWebPartPedigree>"
				Write-Output "</FolderWebPart>"
			}
			Write-Output "</FolderFile>"
		}
		Write-Output "</Folder>"
	}
	foreach($file in $web.files | where-object{$_.Name -match "*.aspx"})
	{
			Write-Output "<SiteFile Name= '>" $file.name "'>"

			#get the file's URL...
			$FileURL = $file.URL

			#get a collection of all the webparts on the page using SPWeb.GetLimitedWebPartManager() method...
			$scope = [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared;
			$WebPartManager = $web.GetLimitedWebPartManager($FileURL, $scope);
			$WebPartCollection = $WebPartManager.WebParts;

			#loop through the web part collection printing out the "WebBrowsableObject" (full name) of each web part...
			foreach($WebPart in $WebPartCollection)
			{
				Write-Output "<FileWebPart>"
				Write-Output "<FileWebPartPedigree>" $WebPart.WebBrowsableObject.ToString() "</FileWebPartPedigree>"
				Write-Output "</FileWebPart>"
			}
			Write-Output "</SiteFile>"			
	}
	Write-Output "</Web>"
}

#########################################################################################

## 20081028 MV - Returns all webparts on all files(webpart pages) of all websites in all site collections in all web applications in The Farm...
function global:get-AllWebPartsInFarmXML()
{
  Write-Output "<WebAppCollection>"
  $WebAppCollection = get-webapplicationcollection;
	foreach($WebApp in $WebAppCollection)
	{
	  Write-Output "<WebApp DisplayName= '" $WebApp.DisplayName "'>"
	  Write-Output "<SiteCollections>"
	  foreach($site in $webApp.Sites)
	  {
		Write-Output "<SiteCollection URL= '" $site.URL "'>"
		Write-Output "<Webs>"
		foreach($web in $site.AllWebs)
		{
			get-AllWebPartsInWebXML($web.URL)
		}
		Write-Output "</Webs>"
		Write-Output "</SiteCollection>"
	  }
	  Write-Output "</SiteCollections>"
	  Write-Output "</WebApp>"
  }
  Write-Output "</WebAppCollection>"
}
#########################################################################################